<template>
	<div class="u-page">
		<div class="u-box">
			<div class="search-width">
				<u-search :show-action="false" placeholder="请输入关键字" v-model="keyword" @search="search"></u-search>
			</div>
			<div class="u-pt-10" v-if="DataItems && DataItems.length > 0">
				<u-radio-group @change="radioGroupChange">
					<u-radio active-color="#eb5468" v-for="(item, index) in DataItems" :key="index" :name="item.goodsNo">
						<div class="u-flex u-row-between u-col-center u-mb-5 u-pt-10 u-pl-10">
							<u-avatar :src="item.goodsImages.split(',')[0]" size="140" mode="square"></u-avatar>
							<div class="u-col-center u-flex-1 u-pl-10">
								<div class="u-title">
									{{item.goodsName}}
								</div>
								<div class="commodity-no">编号 : {{item.goodsNo}}</div>
								<div>商品价格 : {{item.goodsPrice?('￥'+item.goodsPrice):'暂无'}}</div>
							</div>
						</div>
					</u-radio>
				</u-radio-group>
			</div>
			<div v-else>
				<div class="u-text-center" style="padding-top: 20%;">
					<u-empty text="暂无商品" mode="data"></u-empty>
				</div>
			</div>
		</div>
		<div style="height: 210rpx;"></div>
		<!-- 按钮 -->
		<div style="position: fixed;bottom: 0rpx;background-color: #f5f7f7e6;width: 100%;">
			<div class="u-pt-20 u-pb-40" style="width: 90%;margin: 0 auto;">
				<u-button :custom-style="customStyle" shape="square" @click="confirm">
					确定
				</u-button>
			</div>
		</div>

		<!-- 弹窗【选择商品】 -->
		<u-modal ref="uModal" title="平台提示" v-model="show" confirm-text="确认">
			<div class="u-p-20">
				<p>请选择商品！</p>
			</div>
		</u-modal>

	</div>
</template>

<script>
	export default {

		data() {
			return {
				pageNum: 1,
				pageSize: 10,
				DataItems: [],
				SelectedCommodity: {},
				keyword: '',
				searchStr: '',
				DataItemsObj: {},
				show: false,
				customStyle: {
					background: '#eb5468 !important',
					color: '#fff !important',
					border: 'none !important'
				},
				tag: '',// 用于区分是发起什么流程。sample:样品发放流程；（默认测评）
			};
		},

		onLoad(options) {
			//未登录跳转到登录页面
			let loginRes = this.checkLogin();
			if (!loginRes) {
				return false;
			}
			this.iniData();
			
			// 用于区分是发起测试还是发起样品发放
			if(this.isNotNull(options.tag)){
				this.tag = options.tag;
			}
		},

		onShow() {

		},

		onPullDownRefresh() {
			this.pageNum = 1
			this.iniData()
		},

		onReachBottom() {
			this.pageNum += 1
			this.iniData()
		},

		methods: {

			/**
			 * 获取商品列表
			 */
			async iniData() {
				let result = await this.$u.api.unifyMiniRest({
					systemid: "meily",
					url: "/goods/shelf/list",
					searchStr: this.searchStr,
					pageNum: this.pageNum,
					pageSize: this.pageSize,
				});
				if (result.code == 200) {
					if (this.pageNum != 1) {
						this.DataItems = this.DataItems.concat(result.rows)
					} else {
						this.DataItems = result.rows
					}
				} else {
					this.showMSG('error', '出错啦！')
					setTimeout(() => {
						this.goBack()
					}, 1500)
				}
			},

			async search() {
				this.searchStr = this.keyword
				this.pageNum = 1
				this.iniData()
			},

			// 选中任一radio时，由radio-group触发
			radioGroupChange(e) {
				for (let i in this.DataItems) {
					if (this.DataItems[i].goodsNo == e) {
						this.SelectedCommodity = this.DataItems[i]
					}
				}
			},

			/**
			 * 选中确定返回
			 */
			confirm() {
				if(this.tag == 'sample'){
					// 样品发放
					this.ProvideSamples();
				} else {
					// 发起测评
					this.Evaluation();
				}
			},
			
			/**
			 * 测评
			 */
			Evaluation(){
				
				/* if (this.isNotNull(this.SelectedCommodity.goodsNo)) {
					let _good = {};
					
					_good.CommodityNo = this.SelectedCommodity.goodsNo;
					_good.CommodityName = this.SelectedCommodity.goodsName;
					_good.CommodityDetail = this.SelectedCommodity.detail;
					_good.CommodityPicture = this.SelectedCommodity.goodsImages;
					_good.CommodityPrice = this.SelectedCommodity.discountPrice;// 玫丽专享价
					_good.Price = this.SelectedCommodity.goodsPrice;// 市场价
					_good.Industry = "营销宣传";// 如果行业字段为空，则设置默认值
					_good.KeyWord = this.SelectedCommodity.goodsTag;
					_good.EnableDistribution = "0";// 开启分销功能。0:否；1:是
					_good.CouponAmount = "";// 优惠券金额
					_good.SceneAssetsId = this.SelectedCommodity.sceneAssetsId;
					_good.SceneID = this.SelectedCommodity.sceneId;
					_good.CSID = this.SelectedCommodity.openKfid;// 微信客服id
					_good.DistributionReward = this.SelectedCommodity.foremanReward;// 线上服务商分销报酬
					_good.ConsumerShareReward = this.SelectedCommodity.consumerShareReward;// 消费者分享报酬
					_good.MerchantName = this.SelectedCommodity.sceneAssetsName;// 定制名称
					_good.MerchantIcon = this.SelectedCommodity.sceneAssetsId;// 定制ID
					
					let product = JSON.stringify(_good)
					uni.navigateTo({
						url:"/pages_private_scene2/ProductPromote/PlaceOrder?product="+encodeURIComponent(product)
					});
				} else {
					this.show = true;
				} */
				
				if (this.isNotNull(this.SelectedCommodity.goodsNo)) {
					let _good = {};
					_good.goodsId = this.SelectedCommodity.goodsId;
					_good.goodsNo = this.SelectedCommodity.goodsNo;
					_good.goodsName = this.SelectedCommodity.goodsName;
					_good.detail = this.SelectedCommodity.detail;
					_good.goodsImages = this.SelectedCommodity.goodsImages;
					_good.specification = this.SelectedCommodity.specification;
					_good.discountPrice = this.SelectedCommodity.discountPrice;// 玫丽专享价
					_good.goodsPrice = this.SelectedCommodity.goodsPrice;// 市场价
					_good.industry = "营销宣传";// 如果行业字段为空，则设置默认值
					_good.goodsTag = this.SelectedCommodity.goodsTag;
					_good.EnableDistribution = "0";// 开启分销功能。0:否；1:是
					_good.CouponAmount = "";// 优惠券金额
					_good.sceneAssetsId = this.SelectedCommodity.sceneAssetsId;
					_good.sceneId = this.SelectedCommodity.sceneId;
					_good.openKfid = this.SelectedCommodity.openKfid;// 微信客服id
					_good.foremanReward = this.SelectedCommodity.foremanReward;// 线上服务商分销报酬
					_good.consumerShareReward = this.SelectedCommodity.consumerShareReward;// 消费者分享报酬
					_good.MerchantName = this.SelectedCommodity.sceneAssetsName;// 定制名称
					_good.MerchantIcon = "";// 定制图标
					uni.navigateTo({
						url:"/pages_private_scene1/evaluation/place_order?goods=" + encodeURIComponent(JSON.stringify(_good))
					});
				} else {
					this.show = true;
				}
			},
			
			/**
			 * 样品发放
			 */
			ProvideSamples(){
				if (this.isNotNull(this.SelectedCommodity.goodsNo)) {
					let _good = JSON.stringify(this.SelectedCommodity)
					uni.navigateTo({
						url:"/pages_private_scene2/provide_samples/place_order?goods="+encodeURIComponent(_good)
					});
				} else {
					this.show = true;
				}
			},

		},

	};
</script>

<style scoped>
	.u-title {
		color: #585757;
		width: 445rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.commodity-no {
		width: 445rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}
</style>
